package cn.javabb.core.service.biz;

import cn.javabb.core.base.BaseService;
import cn.javabb.core.entity.biz.SitePageDO;
import cn.javabb.core.mapper.biz.SitePageMapper;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;

/**
 * @Author QINB imqinbao@163.com
 * @CreateDate 2018/11/23/023 16:38
 * @Since V1.0
 */
@Service
public class SitePageService extends BaseService<SitePageDO> {

    @Resource
    SitePageMapper sitePageMapper;

    public Page list(String keyword,String page,String size){
        Example example = new Example(SitePageDO.class);
        example.createCriteria().andLike("pageName",keyword);
        example.orderBy("orderNo").desc().orderBy("createTime").desc();
        Page p = null;
        if(null != page && size !=null){
           p = PageHelper.startPage(Integer.parseInt(page),Integer.parseInt(size));
        }else{
            p = PageHelper.startPage(1,10);
        }
        sitePageMapper.selectByExample(example);
        return p;
    }

    public boolean updateState(Integer pageId,Integer state){
        SitePageDO page = new SitePageDO();
        page.setPageId(pageId);
        page.setState(state);
        return this.updateSelective(page)>0?true:false;
    }

    public SitePageDO getPage(String code){
        Example ex = new Example(SitePageDO.class);
        ex.createCriteria().andEqualTo("pageCode",code).orEqualTo("pageId",code);
        return sitePageMapper.selectOneByExample(ex);
    }

}
